<template>
    <el-form :model="searchForm" class="mb-3" size="small">
  <el-row>
    <el-col :span="6">
      <el-form-item label="搜索">
        <el-select v-model="searchForm.keyword" placeholder="請選擇">
          <el-option
            v-for="item in tabbars"
            :key="item.id"
            :label="item.name"
            :value="item.num"
          />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item label="時間選擇">
        <el-date-picker
          v-model="searchForm.time"
          type="daterange"
          range-separator="至"
          start-placeholder="開始時間"
          end-placeholder="結束時間"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
    </el-col>
    <el-col :span="6" :offset="6">
      <el-button type="primary" @click="getData">搜索</el-button>
      
    </el-col>
  </el-row>
</el-form>
        
<el-row :gutter="20" class="mt-5">
   <el-col :span="24">
    <div id="chart" style="width:100% ;height:500px"></div>
   </el-col>
</el-row>

<el-button size="small" @click="ex(searchForm.keyword)">導出Excel</el-button>
<el-button  type="success" size="small" @click="pd=(searchForm.keyword)">導出PDF</el-button>
</template>

<script setup>
import {ref,onMounted,onBeforeUnmount} from 'vue'
import * as echarts from 'echarts';
import { round,dowexc,dowpdf } from "@/api/product.js";
// import { useInitTable } from "~/composables/useCommon.js";
const searchForm=ref({
keyword:'',
time:[]
})


var myChart = null
onMounted(() => {
var chartDom = document.getElementById('chart');

if(chartDom){
    myChart = echarts.init(chartDom);
    getData()
}


})

onBeforeUnmount(()=>{
if(myChart) echarts.dispose(myChart)

})

const  getData=()=>{

let option = {
xAxis: {
type: 'category',
data: []
},
yAxis: {
type: 'value'
},
tooltip: { // 配置提示框
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
series: [
{
  data: [],
  type: 'bar',
  barWidth: 30,
  showBackground: true,
  backgroundStyle: {
    color: 'rgba(180, 180, 180, 0.2)'
  }
}
]
};

// option && myChart.setOption(option);

round(searchForm.value.keyword,searchForm.value.time).then(res=>{
res.map((item)=>{
option.xAxis.data.push(item.name)
option.series[0].data.push(item.number)

})

myChart.setOption(option)
})
}

function ex(params){
  
  dowexc(params).then(res=>{
    
    window.open(res)
  })
}

function pd(params){
  dowpdf(params).then(res=>{
    
    window.open(res)
  })
}

const tabbars = [
{
num: "1",
name: "銷售量",
},
{
num: "2",
name: "銷售額",
},
{
num: "3",
name: "瀏覽量",
},
{
num: "4",
name: "庫存量",
},
];


</script>